package cn.sccdlg.ssm.dao;

import cn.sccdlg.ssm.domain.Permission;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author RaoShuang
 * @emil 991207823@qq.com
 * @date 2020/10/27 21:25
 * 权限持久层
 */
@Repository
@Mapper
public interface IPermissionDao {

    /**
     * 查询与role关联的所有的权限
     * @param roleId 角色id
     */
    @Select("SELECT * FROM permission WHERE id IN (SELECT permissionId FROM role_permission WHERE roleId=#{roleId} )")
    List<Permission> findPermissionByRoleId(String roleId) throws Exception;

    /**
     * 查询所有权限信息
     */
    @Select("SELECT * FROM permission")
    List<Permission> findAll() throws Exception;

    /**
     * 保存权限资源信息
     * @param permission 权限资源信息
     */
    @Insert("INSERT INTO permission(id,permissionName,url) VALUES(#{id},#{permissionName},#{url})")
    void save(Permission permission) throws Exception;

    /**
     * 根据权限id查询权限资源信息
     * @param id 权限id
     */
    @Select("SELECT * FROM permission WHERE id=#{id}")
    Permission findById(String id) throws Exception;

    /**
     * 根据权限id删除权限与角色关联关系
     * @param id 权限id
     */
    @Delete("DELETE FROM role_permission WHERE permissionId=#{id}")
    void deleteFromRole2Permission(String id) throws Exception;

    /**
     * 通过权限id删除权限资源信息
     * @param id 权限id
     * @throws Exception
     */
    @Delete("DELETE FROM permission WHERE id=#{id}")
    void deleteById(String id) throws Exception ;
}
